package pdai.jvm.a_深浅拷贝;

import lombok.Data;

import java.io.*;

@Data
public class Major implements Serializable {
    private String majorId;
    private String majorName;

    public Major(String majorId, String majorName) {
        this.majorId = majorId;
        this.majorName = majorName;
    }

    @Override
    public String toString() {
        return "Major{" +
                "majorId='" + majorId + '\'' +
                ", majorName='" + majorName + '\'' +
                '}';
    }

    public static void serialize() throws IOException {
        Major major = new Major("1111", "软件工程11");
        major.setMajorId("111");
        major.setMajorName("软件工程");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("major.txt")));
        objectOutputStream.writeObject("major.txt");
        objectOutputStream.close();
        System.out.println("序列化成功！已经生成student.txt文件");
        System.out.println("==============================================");
    }

    public static void deserialize() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("major.txt")));
        Major major = (Major) objectInputStream.readObject();
        objectInputStream.close();
        System.out.println("反序列化结果为：");
        System.out.println(major);

    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Major.serialize();
        Major.deserialize();
    }
}

